//package com.hundun.vision.biz.service.impl;
//
//import com.hundun.vision.biz.core.ContractBarSeriesManager;
//import com.hundun.vision.biz.core.ContractStrategy;
//import com.hundun.vision.biz.mapper.BackTestMapper;
//import com.hundun.vision.biz.po.BackTestPO;
//import com.hundun.vision.biz.service.BackTestService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Component;
//import org.ta4j.core.AnalysisCriterion;
//import org.ta4j.core.BarSeries;
//import org.ta4j.core.TradingRecord;
//import org.ta4j.core.analysis.cost.CostModel;
//import org.ta4j.core.analysis.cost.FixedTransactionCostModel;
//import org.ta4j.core.analysis.cost.ZeroCostModel;
//import org.ta4j.core.criteria.MaximumDrawdownCriterion;
//import org.ta4j.core.criteria.PositionsRatioCriterion;
//import org.ta4j.core.criteria.ReturnOverMaxDrawdownCriterion;
//import org.ta4j.core.criteria.VersusEnterAndHoldCriterion;
//import org.ta4j.core.criteria.pnl.ProfitLossCriterion;
//import org.ta4j.core.criteria.pnl.ProfitLossPercentageCriterion;
//import org.ta4j.core.criteria.pnl.ReturnCriterion;
//import org.ta4j.core.num.Num;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.math.RoundingMode;
//
///**
// * @author ：RuoChen
// * @date ：10:28 2021/6/25
// * @description：回测服务实现类
// */
//@Slf4j
//@Component
//public class BackTestServiceImplCopy implements BackTestService {
//
//    @Resource
//    private BackTestMapper backTestMapper;
//
//    @Override
//    public void test(ContractStrategy strategy, BarSeries barSeries, String symbol, String interval, BigDecimal leverage) {
//        if (barSeries != null && barSeries.getBarCount() > 0) {
//            CostModel transactionCostModel = new FixedTransactionCostModel(0.0005);
//            CostModel holdingCostModel = new ZeroCostModel();
//            ContractBarSeriesManager seriesManager = new ContractBarSeriesManager(barSeries, strategy, leverage, transactionCostModel, holdingCostModel);
//            TradingRecord tradingRecord = seriesManager.run();
//            if (tradingRecord != null) {
//                AnalysisCriterion profitLossCriterion = new ProfitLossCriterion();
//                AnalysisCriterion profitLossPercentageCriterion = new ProfitLossPercentageCriterion();
//                AnalysisCriterion maxWithdrawCriterion = new MaximumDrawdownCriterion();
//                AnalysisCriterion profitWithdrawCriterion = new ReturnOverMaxDrawdownCriterion();
//                AnalysisCriterion winRatioCriterion = new PositionsRatioCriterion(AnalysisCriterion.PositionFilter.PROFIT);
//                AnalysisCriterion buyHoldCriterion = new VersusEnterAndHoldCriterion(new ReturnCriterion());
//                Num profitResult = profitLossCriterion.calculate(barSeries, tradingRecord);
//                Num profitPercentageResult = profitLossPercentageCriterion.calculate(barSeries, tradingRecord);
//                Num maxWithdrawResult = maxWithdrawCriterion.calculate(barSeries, tradingRecord);
//                Num profitWithdrawResult = profitWithdrawCriterion.calculate(barSeries, tradingRecord);
//                Num winResult = winRatioCriterion.calculate(barSeries, tradingRecord);
//                Num buyHoldResult = buyHoldCriterion.calculate(barSeries, tradingRecord);
//                BackTestPO backTestPO = new BackTestPO();
//                backTestPO.setSymbol(symbol);
//                backTestPO.setTimes(tradingRecord.getPositionCount());
//                backTestPO.setTimeInterval(interval);
//                backTestPO.setStrategyName(strategy.getName());
//                if (!profitResult.isNaN()) {
//                    BigDecimal profit = profitResult.bigDecimalValue()
//                            .setScale(2, RoundingMode.DOWN);
//                    backTestPO.setProfit(profit);
//                }
//                if (!profitPercentageResult.isNaN()) {
//                    BigDecimal profitRatio = profitPercentageResult.bigDecimalValue().multiply(leverage)
//                            .setScale(2, RoundingMode.DOWN);
//                    backTestPO.setProfitRatio(profitRatio);
//                }
//                if (!maxWithdrawResult.isNaN()) {
//                    BigDecimal maxWithdrawRatio = maxWithdrawResult.bigDecimalValue().multiply(BigDecimal.valueOf(100))
//                            .setScale(2, RoundingMode.DOWN);
//                    backTestPO.setMaxWithdrawRatio(maxWithdrawRatio);
//                }
//                if (!profitWithdrawResult.isNaN()) {
//                    BigDecimal profitWithdrawRatio = profitWithdrawResult.bigDecimalValue()
//                            .setScale(2, RoundingMode.DOWN);
//                    backTestPO.setProfitWithdrawRatio(profitWithdrawRatio);
//                }
//                if (!winResult.isNaN()) {
//                    BigDecimal winRatio = winResult.bigDecimalValue()
//                            .setScale(2, RoundingMode.DOWN)
//                            .multiply(BigDecimal.valueOf(100));
//                    backTestPO.setWinRatio(winRatio);
//                }
//                if (!buyHoldResult.isNaN()) {
//                    BigDecimal buyHoldRatio = buyHoldResult.bigDecimalValue()
//                            .setScale(2, RoundingMode.DOWN);
//                    backTestPO.setBuyHoldRatio(buyHoldRatio);
//                }
//                backTestMapper.insertSelective(backTestPO);
//            }
//        }
//    }
//}
